package cn.com.service.impl;

import cn.com.Util.CopyUtils;
import cn.com.Util.StringUtil;
import cn.com.domain.WorkDiaryEntity;
import cn.com.domain.dto.WorkDiaryDTO;
import cn.com.domain.vo.WorkDiaryVo;
import cn.com.mapper.WorkDiaryMapper;
import cn.com.response.BaseResponse;
import cn.com.response.BaseResponseStatus;
import cn.com.service.WorkDiaryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

/**
 * @Author CRL
 * @Date 2021/4/11 23:33
 */
@Service
public class WorkDiaryServiceImpl extends ServiceImpl<WorkDiaryMapper, WorkDiaryEntity> implements WorkDiaryService {

    public static ArrayList<WorkDiaryVo> blockchain = new ArrayList<WorkDiaryVo>();
    public static int difficulty = 5;

    @Override
    public BaseResponse saveWorkDiary(WorkDiaryDTO requestDto) {
        WorkDiaryEntity workDiaryEntity = new WorkDiaryEntity();
        CopyUtils.copyProperties(requestDto, workDiaryEntity);
        String workContent = workDiaryEntity.getWorkContent();
        addBlock(new WorkDiaryVo(workContent, "0"));
        String blockchainJson = StringUtil.getJson(blockchain);
        workDiaryEntity.setNoobChain(blockchainJson);
        boolean save = this.save(workDiaryEntity);
        if (save) {
            return new BaseResponse(workDiaryEntity);
        } else {
            return new BaseResponse<>(BaseResponseStatus.FAILURE, "保存失败");
        }
    }

    public void addBlock(WorkDiaryVo newBlock) {
        newBlock.mineBlock(difficulty);
        blockchain.add(newBlock);
    }


}
